﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Framework.DomainModel.Repositories;
using Resto.CostControl.Common.Utility;

namespace Resto.CostControl.Core.Domain.Model
{
    /// <summary>
    /// 原材料
    /// </summary>
    public partial class RawMaterial : Material
    {
        #region 构造函数

        internal RawMaterial() {
            this.ID = IDHelper.CreateNewID();
        }

        #endregion
        
        #region 私有属性

        private IEntityRepository<RawMaterial> _Repository;
        private IEntityRepository<RawMaterial> Repository
        {
            get
            {
                if (_Repository == null)
                    _Repository = RestoCostDomainSession.CurrentSession.CreateEntityRepository<RawMaterial,Material>();

                return _Repository;
            }
        }

        #endregion

        #region 业务方法

        internal override void Add()
        {
            Validate();

            this.CreateAudit = new Audit();
            this.LastUpdateAudit = new Audit();

            this.Repository.Add(this);
        }

        internal override void Update()
        {
            Validate();

            this.LastUpdateAudit = new Audit();

            this.Repository.Update(this);
        }

        internal override void Delete()
        {
            this.LastUpdateAudit = new Audit();

            this.IsDelete = true;
            this.Repository.Update(this);
        }

        internal override void UpdatePrice(decimal price)
        {
            if (CurrentPrice == price)
                return;

            this.CurrentPrice = price;

            //更新品名的价格
            foreach (SemiMaterial semi in this.SemiMaterials)
                semi.CreatePrice();
        }

        #endregion


    }
}
